package com.bootdo.wx.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.wx.domain.MemberCouponDO;
import com.bootdo.wx.domain.MemberUserDO;
import com.bootdo.wx.domain.OrderOrderDO;
import com.bootdo.wx.service.MemberCouponService;
import com.bootdo.wx.service.MemberUserService;
import com.bootdo.wx.service.OrderOrderService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.StringUtils;

/**
 * 用户优惠券
 * 
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2019-06-13 15:54:40
 */
 
@Controller
@RequestMapping("/wx/memberCoupon")
public class MemberCouponController {
	@Autowired
	private MemberCouponService memberCouponService;
	@Autowired
	private MemberUserService memberUserService;
	@Autowired
	private OrderOrderService orderOrderService;
	
	@GetMapping()
	@RequiresPermissions("wx:memberCoupon:memberCoupon")
	String MemberCoupon(){
	    return "wx/memberCoupon/memberCoupon";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("wx:memberCoupon:memberCoupon")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
		params.put("delFlag", "0");
		params.put("sort", "create_at");
		params.put("order", "desc");
		params.put("status", "1");
        Query query = new Query(params);
		List<MemberCouponDO> memberCouponList = memberCouponService.list(query);
		List<Map<String, Object>> list = new ArrayList<>();
		if(memberCouponList.size()>0){
			for (MemberCouponDO memberCouponDO : memberCouponList) {
				Map<String, Object> entityToMap = StringUtils.entityToMap(memberCouponDO);
				MemberUserDO memberUserDO = memberUserService.get(memberCouponDO.getUserId());
				if(memberUserDO!=null){
					entityToMap.put("userName", memberUserDO.getName());
					entityToMap.put("userPhone", memberUserDO.getPhone());
				}else{
					entityToMap.put("userName", "");
					entityToMap.put("userPhone", "");
				}
				OrderOrderDO orderOrderDO = orderOrderService.get(memberCouponDO.getOrderId());
				if(orderOrderDO!=null){
					entityToMap.put("payment", orderOrderDO.getPayment());
				}else{
					entityToMap.put("payment", "");
				}
				list.add(entityToMap);
			}
		}
		int total = memberCouponService.count(query);
		PageUtils pageUtils = new PageUtils(list, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("wx:memberCoupon:add")
	String add(){
	    return "wx/memberCoupon/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("wx:memberCoupon:edit")
	String edit(@PathVariable("id") String id,Model model){
		MemberCouponDO memberCoupon = memberCouponService.get(id);
		model.addAttribute("memberCoupon", memberCoupon);
	    return "wx/memberCoupon/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("wx:memberCoupon:add")
	public R save( MemberCouponDO memberCoupon){
		if(memberCouponService.save(memberCoupon)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("wx:memberCoupon:edit")
	public R update( MemberCouponDO memberCoupon){
		memberCouponService.update(memberCoupon);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("wx:memberCoupon:remove")
	public R remove( String id){
		if(memberCouponService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("wx:memberCoupon:batchRemove")
	public R remove(@RequestParam("ids[]") String[] ids){
		memberCouponService.batchRemove(ids);
		return R.ok();
	}
	
	
	/**
	 * 获取优惠券总发放数量
	 */
	@ResponseBody
	@PostMapping("/getNumByUserT")
	@RequiresPermissions("wx:memberCoupon:memberCoupon")
	public R getNumByUserT(@RequestParam Map<String, Object> params){
		int count = memberCouponService.count(params);
		return R.ok().put("data",count);
	}
	
}
